package com.example.worldcupsever;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.worldcupsever.bean.User;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "RegistActivity";
    EditText mEtName, mEtEmail, mEtPwd, mEtRepwd;
    Button mBtnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        init();
    }

    private void init() {
        mEtName = findViewById(R.id.et_user_name);
        mEtEmail = findViewById(R.id.et_email);
        mEtPwd = findViewById(R.id.et_pwd);
        mEtRepwd = findViewById(R.id.et_repwd);
        mBtnRegister = findViewById(R.id.btn_register);
        mBtnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                String name=mEtName.getText().toString().trim();
                String email=mEtEmail.getText().toString().trim();
                String password=mEtRepwd.getText().toString().trim();
                String repassword=mEtRepwd.getText().toString().trim();

                if (TextUtils.isEmpty(name)||TextUtils.isEmpty(password)||TextUtils.isEmpty(repassword)){
                    Toast.makeText(this, "请输入完全", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!password.equals(repassword)){
                    Toast.makeText(this, "两次密码不一样", Toast.LENGTH_SHORT).show();
                    return;
                }
                OkHttpClient client=new OkHttpClient();
                JSONObject object=new JSONObject();
                try {
                    object.put("name",name);
                    object.put("email",email);
                    object.put("password",password);
                    object.put("repassword",repassword);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json=object.toString();

                RequestBody body= FormBody.create(MediaType.parse("application/json;charset=utf-8"),json);
                Request request=new Request.Builder()
                        .url("http://192.168.1.104:8080/WorldCupApp/user/register")
                        .post(body)
                        .build();

                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Log.e(TAG, "onFailure: "+e );
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String jsonStr = response.body().string();
                        Log.e(TAG, "onResponse: "+jsonStr );
                        Gson gson=new Gson();
                        //解析
                        User user = gson.fromJson(jsonStr, User.class);
                    }
                });
                finish();
                Intent intent=new Intent(RegistActivity.this, MainActivity.class);
                startActivity(intent);
                break;

        }
    }
}
